<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form id="myForm">
姓名: <input type="text" name="name" value="王**"><br>
性别: <input type="radio" name="sex" value="男" checked="true">男
	  <input type="radio" name="sex" value="女">女<br>
年龄: <input type="text" name="age" value="28"><br>
住址: <input name="address" value="山东"><br>
</form>
<p>点击以下按钮，显示form元素中所有元素的数量</p>
<button onclick="myFc()">按钮</button>
<p id="demo"></p>
<script>
function myFc() {
	var obj = document.getElementById("myForm").elements;
	var len = document.getElementById("myForm").elements.length;
	var tmp = "";
	var tmp_value = "";
	for(var i=0;i<len;i++){
		 if (obj[i].hasAttribute("name")){
		   tmp += obj[i].name + " ";
		   tmp_value += obj[i].value + " ";
		 }
	   }    
	document.getElementById("demo").innerHTML = "form元素中有 " + len + " 个元素"+"<br>元素名称分别为："+tmp+"<br>元素值分别为："+tmp_value;
}
</script>
</body>
</html>